package NewTmas.EditMode;

import NewTmas.FramesandPanels.LoginFrame;
import NewTmas.Listeners.CascadeWindowStateListener;
import NewTmas.Listeners.TileWindowStateListener;
import NewTmas.Listeners.WindowOpenListener;
import taskmaster.auth.User;
import taskmaster.util.LookupLists;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/1/11
 * Time: 2:22 PM
 * To change this template use File | Settings | File Templates.
 */
public class EditModeMainFrame extends JFrame
{
    JDesktopPane dtp;
    JMenuBar menuBar;
    JMenu window;
    User user;
    LookupLists ll;

    public EditModeMainFrame(User u, LookupLists ll)
    {


        this.user = u;
        this.ll = ll;

        createFrame();

    }

    private void createFrame()
    {
        setTitle("Edit Mode for " + user.getUserNameDatabaseString());
        setSize(1100, 950);
        setLocation(20, 20);
        dtp = new JDesktopPane();
        setContentPane(dtp);
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu file = new JMenu("File");
        window = new JMenu("Window");

        JMenu arrangeWindows = new JMenu("Arrange Windows");
        JCheckBoxMenuItem tile = new JCheckBoxMenuItem("Tile");
        JCheckBoxMenuItem cascade = new JCheckBoxMenuItem("Cascade");
        arrangeWindows.add(tile);
        arrangeWindows.add(cascade);
        JMenu windowMenu = new JMenu("Window");
        windowMenu.add(window);
        JMenuItem newJobs = new JMenuItem("Open Job for Edit");
        file.add(newJobs);

        windowMenu.add(arrangeWindows);
        newJobs.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                new EditModeJobOpenerFrame(user, dtp, EditModeMainFrame.this.ll);

            }
        });


        JMenuItem logout = new JMenuItem("Logout");
        file.add(logout);
        logout.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {

                user.logout();
                LoginFrame l = new LoginFrame("", "", "");
                dispose();
            }
        });


        menuBar.add(file);
        menuBar.add(windowMenu);

        tile.addItemListener(new TileWindowStateListener(dtp, tile));
        cascade.addItemListener(new CascadeWindowStateListener(dtp, cascade));

        JMenuItem menuitem_exit;
        file.add(menuitem_exit = new JMenuItem("Exit"));

        window.addMenuListener(new WindowOpenListener(this.window, dtp));


        menuitem_exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    user.getConnection().close();
                } catch (SQLException e1)
                {
                    JOptionPane.showMessageDialog(null, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                    // e1.printStackTrace();

                }
                System.exit(0);
            }
        });


        setVisible(true);
    }


}
